<?php
header ('Content-type: text/html; charset=utf-8');
define('DS', DIRECTORY_SEPARATOR);
defined('APP_PATH') ? null : define('APP_PATH', '..' . DS . 'application');

require_once (APP_PATH . DS . 'initialize.php');

if (User::is_have_user())
{
	redirect_to("index.php");
}
?>
<?php
// Remember to give your form's submit tag a name="submit" attribute!
if (isset($_POST['submit'])) { // Form has been submitted.

	$username = trim($_POST['username']);
	$password = trim($_POST['password']);
	
	
	if($username != "")
	{
		if($password != "")
		{
			// Check database to see if username/password exist.
			$userObj = new User();
			$found_user = $userObj->create_user($username, $password);

			if ($found_user) {
				$_SESSION['empty_user'] = false;
				$session->login($found_user);
				//log_action('Login', "{$found_user->username} logged in.");
				//$message = "Log in success";
				redirect_to("index.php");
			} else {
				//Can not create new user.
				$message = "Can not create new user.";
			}
		}else{
			//Username is empty.
			$message = "Please fill username.";
		}
	}else{
		//Password is empty.
		$message = "Please fill password.";
	}

} else { // Form has not been submitted.
	$username = "";
	$password = "";
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="snopeborn" />

<title>Login</title>
</head>

<body>

<?php

//include_layout_template('admin_header.php');

?>

<h2>Create New User</h2>
<?php echo output_message($message); ?>

<form action="create_user.php" method="post">
<table>
	<tr>
		<td>ชื่อผู้ใช้:</td>
		<td><input type="text" name="username" maxlength="30"
			value="<?php echo htmlentities($username); ?>" /></td>
	</tr>
	<tr>
		<td>Password:</td>
		<td><input type="password" name="password" maxlength="30"
			value="<?php echo htmlentities($password); ?>" /></td>
	</tr>
	<tr>
		<td colspan="2"><input type="submit" name="submit" value="Create" /></td>
	</tr>
</table>
</form>

<?php

//include_layout_template('admin_footer.php');

?>
</body>
</html>
